package com.javaclimb.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.javaclimb.drug.common.ResultMapUtil;
import com.javaclimb.drug.entity.Classify;
import com.javaclimb.drug.service.IClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/classify")
public class ClassifyController {
    @Autowired
    private IClassifyService classifyService;

        //    转向药品分类页面
        @RequestMapping
        public String classify(){

            return "/classify";
        }
        /*
        * 分页查询药品分类列表*/
        @RequestMapping(value = "/classifyQueryPage")
        @ResponseBody
    public Object classifyQueryPage(String param, @RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int limit){
        try {
            IPage<Classify> iPage = classifyService.selectClassifyPage(page,limit,param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }
//    转向药品分类新增页面
    @RequestMapping(value = "/classifyPage")
    public String classifyPage(){
            return "/classifyPage";
    }
//    添加药品分类
    @RequestMapping(value = "/classifyAdd")
    @ResponseBody
    public Object classifyAdd(Classify classify){
        try {
            int i=classifyService.addClassify(classify);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }

    }
//    转向药品分类编辑页面
    @RequestMapping(value = "classifyQueryById")
    public String classifyQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        Classify classify = classifyService.queryClassifyById(id);
        model.addAttribute("obj",classify);
        return "/classifyPage";
    }
    @RequestMapping(value = "/classifyEdit")
    @ResponseBody
    public Object classifyEdit(Classify classify){
            try{
                int i = classifyService.editClassify(classify);
                return ResultMapUtil.getHashMapSave(i);
            }catch (Exception e){
                return ResultMapUtil.getHashMapException(e);
        }
    }
//    删除一个药品分类
   @RequestMapping(value = "/classifyDelById")
   @ResponseBody
   public Object classifyDelById(Integer id){
       try {
           int i = classifyService.delClassifyById(id);
           return ResultMapUtil.getHashMapDel(i);
       } catch (Exception e) {
           return ResultMapUtil.getHashMapException(e);
       }
   }
//   获取所有药品分类
    @RequestMapping(value = "/classifyList")
    @ResponseBody
    public Object classifyList(){
        List<Classify> classifysList = classifyService.queryClassifyList();
        return ResultMapUtil.getHashMapList(classifysList);
    }

}
